package com.wing.user.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.PageResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.contract.model.entity.WarehouseContractFee;
import com.wing.contract.model.entity.WarehouseContractProduct;
import com.wing.contract.model.request.WarehouseContractForm;
import com.wing.contract.model.response.WarehouseContractVO;
import com.wing.contract.service.WarehouseContractFeeService;
import com.wing.contract.service.WarehouseContractProductService;
import com.wing.product.dao.WarehouseSettledDao;
import com.wing.product.model.response.WarehouseOrderVO;
import com.wing.product.model.response.WarehouseOutboundOrderVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.contract.dao.WarehouseContractDao;
import com.wing.contract.model.entity.WarehouseContract;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseContractServiceImpl extends ServiceImpl<WarehouseContractDao, WarehouseContract> implements WarehouseContractService{

    @Autowired
    private WarehouseSettledDao warehouseSettledDao;

    @Autowired
    private WarehouseContractProductService warehouseContractProductService;

    @Autowired
    private WarehouseContractFeeService warehouseContractFeeService;

    @Override
    public Boolean save(WarehouseContractForm warehouseContractForm) {

        WarehouseContract warehouseContract =new WarehouseContract();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseContract.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseContractForm, warehouseContract, copyOptions);
        warehouseContract.setContractCode(UniqueIdGeneratorUtil.generateUniqueId("TGHT", "yyyyMMddHHmmss", 1000, 9999));
        warehouseContract.setContractTime(LocalDateTime.now());
        warehouseContract.setApprover(warehouseContractForm.getApproverId());
        warehouseContract.setContractor(warehouseContractForm.getContractorId());
        if(warehouseContractForm.getContractPrice().isEmpty()){
            warehouseContract.setContractPrice("0");
        }
        return this.save(warehouseContract);
    }

    @Override
    public Boolean saveProduce(WarehouseContractForm warehouseContractForm) {

        WarehouseContract warehouseContract =new WarehouseContract();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseContract.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseContractForm, warehouseContract, copyOptions);
        warehouseContract.setContractCode(UniqueIdGeneratorUtil.generateUniqueId("SCHT", "yyyyMMddHHmmss", 1000, 9999));
        warehouseContract.setContractTime(LocalDateTime.now());
        warehouseContract.setApprover(warehouseContractForm.getApproverId());
        warehouseContract.setContractor(warehouseContractForm.getContractorId());
        warehouseContract.setContactId(warehouseContractForm.getContactId());
        warehouseContract.setWarehouseCustomerId(warehouseContractForm.getWarehouseCustomerId());
        if(StringUtils.isEmpty(warehouseContractForm.getContractPrice())){
            warehouseContract.setContractPrice("0");
        }

        this.save(warehouseContract);

        //新增服务项的关联记录
        for (int i = 0; i < warehouseContractForm.getWarehouseFeeItemsList().size(); i++) {
            WarehouseContractFee warehouseContractFee=new WarehouseContractFee();
            warehouseContractFee.setContractId(warehouseContract.getId());
            warehouseContractFee.setContractPrice(Double.parseDouble(warehouseContractForm.getWarehouseFeeItemsList().get(i).getPrice().toString()));
            warehouseContractFee.setMemberPrice(Double.parseDouble(warehouseContractForm.getWarehouseFeeItemsList().get(i).getPrice().toString()));
            warehouseContractFee.setProductFeeId(warehouseContractForm.getWarehouseFeeItemsList().get(i).getId());
            warehouseContractFeeService.save(warehouseContractFee);
        }

        //新增托管单价关联记录（牌号，钢种）
        for (int i = 0; i < warehouseContractForm.getWarehouseProductVarietyList().size(); i++) {
            WarehouseContractProduct warehouseContractProduct = new WarehouseContractProduct();
            warehouseContractProduct.setContractId(warehouseContract.getId());
            warehouseContractProduct.setContractPrice(warehouseContractForm.getWarehouseProductVarietyList().get(i).getContractPrice());
            warehouseContractProduct.setMemberPrice(warehouseContractForm.getWarehouseProductVarietyList().get(i).getMemberPrice());
            warehouseContractProduct.setProductVarietyId(warehouseContractForm.getWarehouseProductVarietyList().get(i).getId());
            warehouseContractProductService.save(warehouseContractProduct);
        }

        return true;
    }

    @Override
    public Boolean update(WarehouseContractForm warehouseContractForm) {
        WarehouseContract warehouseContract =new WarehouseContract();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseContract.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseContractForm, warehouseContract, copyOptions);
        warehouseContract.setApprover(warehouseContractForm.getApproverId());
        warehouseContract.setContractor(warehouseContractForm.getContractorId());
        if(warehouseContractForm.getContractPrice().isEmpty()){
            warehouseContract.setContractPrice("0");
        }
        baseMapper.updateById(warehouseContract);
        return true;
    }

    @Override
    public Page<WarehouseContractVO> pageList4app(Page page, Map<String, Object> params) {

//        return new PageResult<>(baseMapper.list4app(pg, paramMap));
        List<WarehouseContractVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WarehouseContractVO info(Long id) {
        WarehouseContractVO warehouseContractVO=baseMapper.info(id);
        warehouseContractVO.setWarehouseContractFeeVOList(warehouseSettledDao.findFeeByContactId(id));
        warehouseContractVO.setWarehouseContractProductVOList(warehouseSettledDao.findVarietyByContactId(id));
        return warehouseContractVO;
    }

    @Override
    public List<WarehouseContract> findByCustomerId(Long customerId) {
        return baseMapper.findByCustomerId(customerId);
    }


}
